'use strict';
const ora = require('ora');
const chalk = require('chalk');
const download = require('./download');
const path = require('./path');

// 下载需要配置的库文件
module.exports = async function(answers, productMap) {
    const spinner = ora(' 开始远程下载配置cli...').start();
    // 下载cli
    await download({
        git: answers.git,
        npm: answers.npm,
        path: `${path.rootPath}${path.cli()}`
    });
    // 下载必须要的组件
    if(answers.default) {
        const defaultPg = answers.default;
        // 批量MQ
        let mq = [];
        Object.keys(defaultPg).forEach(function(d) {
            mq.push(new Promise(res => {
                download({
                    git: defaultPg[d].git,
                    npm: defaultPg[d].npm,
                    path: `${path.rootPath}${path.default()}/${d}`
                }).then(() => res());
            }));
        });
        await Promise.all(mq);
    }
    // 下载可选择的组件
    if(answers.choice) {
        const choicePg = answers.choice;
        let mq = [];
        Object.keys(choicePg).forEach(function(d) {
            mq.push(new Promise(res => {
                download({
                    git: choicePg[d].git,
                    npm: choicePg[d].npm,
                    path: `${path.rootPath}${path.choice()}/${d}`
                }).then(() => res());
            }));
        });
        await Promise.all(mq);
    }
    spinner.stop();
    console.log(chalk.green("远程组件下载成功！"));
    return true;
};